<template>
  <div class="custom-content">
    <div
      v-if="
        setting.query !== undefined &&
          setting.query !== null &&
          setting.query.length > 0
      "
      class="custom-header"
    >
      <div
        v-for="(item, index) in setting.query"
        :key="'input-' + index"
        class="header-input"
      >
        <div class="header-input-span">
          <input
            v-if="item.type === 1"
            v-model="form[item.column]"
            placeholder="请输入查询内容"
            @keyup="queryObject"
          >&nbsp;&nbsp;&nbsp;
          <div class="header-input-icon" @click="queryObject">
            <i class="el-icon-search" />
          </div>
        </div>
      </div>
    </div>
    <div class="custom-body">
      <el-scrollbar
        style="width: 100%;height: 100%;overflow-x: hidden;overflow-y: auto"
      >
        <el-empty
          v-if="dataList === null || dataList.length === 0"
          :image-size="100"
        />
        <div v-else>
          <div
            v-for="(obj, index) in dataList"
            :key="'obj-' + index"
            class="custom-ul-li"
            :loading="loading"
            @click="openEntity(obj)"
          >
            <div class="custom-ul-li-title">
              {{ obj[column["title"]] }}
            </div>
            <div class="custom-li-button">
              {{
                obj[column["left"]].substring(0, obj[column["left"]].length - 3)
              }}
            </div>
          </div>
          <div class="custom-loading-more">
            <el-button
              v-if="isLoading === true"
              type="text"
              :loading="moreLoading"
              @click="loadingMore"
            >加载更多</el-button>
            <div v-else class="loading-none">没有更多数据</div>
          </div>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script>
import {
  queryAllSystemSys,
  queryAllGuestSystemSys
} from '@/api/portal/manager/analysis';
import { getToken } from '@/utils/auth';
import avatar from '@/assets/images/avatar.png';
export default {
  name: 'AppNewsListTemp',
  props: {
    temp: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      dataList: [],
      setting: {},
      column: {},
      loading: false,
      moreLoading: false,
      defaultImage: avatar,
      pagerSize: 0,
      form: {
        page: 1,
        size: 15,
        cid: null
      }
    };
  },
  computed: {
    isLoading() {
      const pagers =
        Math.ceil((this.pagerSize + this.form.size) / this.form.size) - 1;
      if (this.form.page < pagers) {
        return true;
      } else {
        return false;
      }
    }
  },
  watch: {
    temp: {
      handler(newName, oldName) {
        // 值发生变化时
        if (undefined !== newName && newName !== null) {
          this.setting = JSON.parse(newName.pzx);
          this.column = this.setting;
          this.form.cid = newName.id;
          this.initData(newName.id);
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    loadingMore() {
      const pagers =
        Math.ceil((this.pagerSize + this.form.size) / this.form.size) - 1;
      if (this.form.page < pagers) {
        this.form.page = this.form.page + 1;
        this.initData();
      }
    },
    initData() {
      this.moreLoading = true;
      if (getToken()) {
        queryAllSystemSys(this.form)
          .then(res => {
            const { data } = res;
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = this.dataList.concat(data.dataList);
              this.pagerSize = data.pager.totalElements;
            }
            this.moreLoading = false;
          })
          .catch(() => {
            this.moreLoading = false;
            this.dataList = [];
          });
      } else {
        queryAllGuestSystemSys(this.form)
          .then(res => {
            const { data } = res;
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList;
              this.pagerSize = data.pager.totalElements;
            }
            this.moreLoading = false;
          })
          .catch(() => {
            this.moreLoading = false;
            this.dataList = [];
          });
      }
    },
    queryObject() {
      this.form.page = 1;
      this.dataList = [];
      this.initData();
    },
    openEntity(data) {
      // 打开详情页
      if (this.column.isOpenEntity) {
        // 允许打开详情页
        const params = [];
        let url = '';
        if (
          undefined !== this.column.entityParams &&
          this.column.entityParams !== null &&
          this.column.entityParams.length > 0
        ) {
          this.column.entityParams.forEach(item => {
            const obj = { code: item.code, value: data[item.value] };
            params.push(obj);
          });
        }
        if (this.column.entityOpenType === 1) {
          // 地址参数的方式打开
          url = data[this.column.entityUrl];
        }
        if (this.column.entityOpenType === 2) {
          // 地址传参查询
          if (
            undefined !== this.column.params &&
            this.column.params !== null &&
            this.column.params.length > 0
          ) {
            url = this.column.entityUrl;
            this.column.params.forEach(item => {
              url = url.replace(
                new RegExp(
                  '\\$\\{([\\s]*?)(' + item.code + '{1})([\\s]*?)([^}])*\\}'
                ),
                data[item.column]
              );
            });
          }
        }
        const object = {
          title: data[this.column['title']],
          params: params,
          setting: this.temp,
          entityTemp: this.column.entityTemplateId,
          temp: this.column.entityTemp,
          openType: this.column.entityOpenType,
          url: url
        };
        this.$emit('fetch-entity', object);
      }
    }
  }
};
</script>

<style scoped>
.custom-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: column nowrap;
}
.custom-header {
  flex: 0 1 auto;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  background-color: #f2f6fc;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
.custom-body {
  position: absolute;
  top: 81px;
  left: 0px;
  right: 0px;
  bottom: 0px;
  width: 100%;
  overflow: hidden;
  padding: 0px 10px;
  box-sizing: border-box;
}
.header-input-span > input {
  width: 100%;
  border: none;
  outline: none;
}
.header-input {
  width: 100%;
  line-height: 40px;
  height: 40px;
  padding: 5px 15px;
  box-sizing: border-box;
  font-size: 13px;
}
.header-input-span {
  width: 100%;
  height: 100%;
  border-radius: 18px;
  background-color: #ffffff;
  padding: 0px 20px;
  box-sizing: border-box;
  position: relative;
  line-height: 30px;
  color: #606266;
}
.header-input-icon {
  position: absolute;
  right: 5px;
  top: 0px;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
}
.custom-ul-li {
  width: 100%;
  height: 40px;
  display: flex;
  flex-flow: row nowrap;
  border-bottom: 1px solid #ebeef5;
  cursor: pointer;
  flex-flow: row nowrap;
  color: #606266;
  margin-bottom: 2px;
  border-radius: 3px;
  background: #f5f7fa;
}
.custom-ul-li-title {
  font-size: 13px;
  flex: 1 1 auto;
  height: 40px;
  line-height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-left: 10px;
}
.custom-ul-li-button {
  flex: 0 1 auto;
  height: 40px;
  display: flex;
  flex-flow: column nowrap;
  color: #909399;
}
.custom-li-button {
  flex: 0 1 auto;
  overflow: hidden;
  line-height: 40px;
  font-size: 13px;
  margin-right: 8px;
}
.custom-li-time {
  flex: 0 1 auto;
  height: 20px;
  line-height: 20px;
  text-align: right;
  font-size: 12px;
}
.custom-loading-more {
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
}
.loading-none {
  font-size: 13px;
  color: #909399;
}
[class*="el-col-"] {
  float: left;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.el-scrollbar >>> .el-scrollbar__wrap {
  overflow-x: hidden;
  overflow-y: auto;
}
</style>
